package com.eden.api.controller;

import com.eden.api.controller.vo.ApiResult;
import com.eden.api.security.JwtUser;
import com.eden.api.security.LoginUser;
import com.eden.common.utils.DateUtils;
import com.eden.common.utils.StringUtils;
import com.eden.school.domain.SchoolUser;
import com.eden.school.domain.SearchHistory;
import com.eden.school.service.ISearchHistoryService;
import com.eden.task.domain.UserTask;
import com.eden.task.service.IUserTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.eden.api.controller.vo.ApiResult.error;
import static com.eden.api.controller.vo.ApiResult.success;

/**
 * search页面的请求
 */
@Slf4j
@Api("搜索任务页面接口")
@RestController
@RequestMapping("/api/search")
public class ApiSearchController {
    @Autowired
    private IUserTaskService userTaskService;
    @Autowired
    private ISearchHistoryService searchHistoryService;

    @ApiOperation("根据登录名查询历史记录")
    @GetMapping("/searchHistory")
    public ApiResult searchHistory(@RequestParam(value = "loginName") String loginName) {
        if (StringUtils.isNotEmpty(loginName)) {
            List<SearchHistory> histories = searchHistoryService.searchHistoryByLoginName(loginName);
            return success("查询历史记录成功", histories);
        } else
            return error("登录名称为空无法查询");
    }

    @ApiOperation("根据登录名删除历史记录")
    @GetMapping("/delSearchHistory")
    public ApiResult delSearchHistory(@RequestParam(value = "loginName") String loginName) {
        if (StringUtils.isNotEmpty(loginName)) {
            searchHistoryService.deleteSearchHistoryByLoginName(loginName);
            return success("删除历史记录成功");
        } else
            return error("登录名称为空无法查询");
    }

    @ApiOperation("根据任务名称查询任务信息")
    @GetMapping("/searchTasksByTaskName")
    public ApiResult searchTasksByTaskName(@RequestParam(value = "taskName") String taskName) {
        if (StringUtils.isNotEmpty(taskName)) {
            UserTask userTask = new UserTask();
            LoginUser loginUser = JwtUser.getInstance().getLoginUser();
            SchoolUser user = loginUser.getUser();
            SearchHistory searchHistory = new SearchHistory();
            searchHistory.setLoginName(user.getLoginName());
            searchHistory.setContent(taskName);
            List<SearchHistory> histories = searchHistoryService.selectSearchHistoryList(searchHistory);
            searchHistory.setCreateTime(DateUtils.getNowDate());
            searchHistory.setCreateBy(user.getUserName());
            if (histories.size() == 0) {
                searchHistoryService.insertSearchHistory(searchHistory);
            }
            userTask.setTaskName(taskName);
            List<UserTask> tasks = userTaskService.selectUserTaskListApi(userTask);
            return success("查询任务信息成功", tasks);
        } else
            return error("搜索的内容为空无法查询");
    }

}
